package com.yuyou.fn.portal.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mq.MessageHandler;
import com.github.wens.mq.RedisMessageQueue;
import com.yuyou.fn.common.util.JSONS;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.portal.repository.OuterCampusRepository;
import com.yuyou.fn.portal.service.IOuterCampusService;
import com.yuyou.fn.portal.vo.OuterCampus;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/18.
 */
@Service
public class OuterCampusServiceImpl implements IOuterCampusService {

    @Resource
    private RedisMessageQueue redisMessageQueue ;

    @Resource
    private OuterCampusRepository outerCampusRepository;

    @Resource
    private MongoTemplate mongoTemplate;

    @PostConstruct
    public void init(){
        redisMessageQueue.consume("campus", "portal", new MessageHandler() {
            @Override
            public void onMessage(byte[] bytes) {
                //System.out.println(JSONS.toJSONObject(bytes));
                JSONObject jsonObject = JSONS.toJSONObject(bytes);
                String event = jsonObject.getString("event");

                if("insert".equals(event) || "update".equals(event)){
                    JSONArray rowList = jsonObject.getJSONArray("rowList");
                    List<OuterCampus> outerCampusList=toOuterCampus(rowList,false);
                    outerCampusRepository.save(outerCampusList);

                }else if("delete".equals(event)){
                    JSONArray rowList = jsonObject.getJSONArray("rowList");
                    List<OuterCampus> outerCampusList=toOuterCampus(rowList,true);
                    outerCampusRepository.delete(outerCampusList);
                }
            }

            private List<OuterCampus> toOuterCampus(JSONArray rowList , boolean isDelete) {
                List<OuterCampus> outerCampusList = new ArrayList<>(rowList.size());
                for (int i = 0; i < rowList.size(); i++) {
                    JSONObject row = processFiled(rowList.getJSONObject(i) );
                    OuterCampus outerCampus = row.toJavaObject(OuterCampus.class);
                    if(!isDelete){
                        fillExtra(outerCampus);
                    }
                    outerCampusList.add(outerCampus);
                }
                return outerCampusList;
            }
            private JSONObject processFiled(JSONObject row) {
                JSONObject nRow = new JSONObject();
                for(String key : row.keySet()){
                    nRow.put(Utils.camelCase(key),row.get(key));
                }
                return nRow ;
            }
        });
    }
    private void fillExtra(OuterCampus outerCampus) {
        String[] locations=outerCampus.getGeoLocation()==null?"0 0".split(" "):outerCampus.getGeoLocation().split(" ");
        if(locations.length>=2){
            Point point=new Point(Double.parseDouble(locations[0]),Double.parseDouble(locations[1]));
            outerCampus.setCampusLocation(point);
        }
    }

    @Override
    public List<OuterCampus> findCampusByPosition(Double distance,Double longitude, Double latitude) {
        Criteria criteria = new Criteria();
        Query query=new Query();
        Point point=new Point(longitude,latitude);
        query.addCriteria(criteria.where("campusLocation").near(point).maxDistance(distance/1000/111));//公里数除以111算出弧度
        return   mongoTemplate.find(query,OuterCampus.class);
    }
}
